Inside Macintosh: Memory

Previous | Chapter Top | Chapter Contents | Next

Heap Zones

Except for temporary memory blocks, all relocatable and nonrelocatable blocks exist within heap zones. A heap zone consists of a zone header, a zone trailer block, and usable bytes in between. The header contains all of the information the Memory Manager needs about that heap zone; the trailer is just a minimum-sized free block placed as a marker at the end of the zone.

In Pascal, a heap zone is defined as a zone record of type Zone . The zone record contains all of the fields of the zone header. A heap zone is always referred to with a zone pointer of data type THz .

The fields of the zone header are for the Memory Manager's own internal use. You can examine the contents of the zone's fields, but in general it doesn't make sense for your application to try to change them. The only fields of the zone record that you can safely modify directly are the moreMast and purgeProc fields.

TYPE Zone =
RECORD
    bkLim:              Ptr;                {first usable byte after zone}
    purgePtr:           Ptr;                {used internally}
    hFstFree:           Ptr;                {first free master pointer}
    zcbFree:            LongInt;            {number of free bytes in zone}
    gzProc:             ProcPtr;            {grow-zone function}
    moreMast:           Integer;            {num. of master ptrs to allocate}
    flags:              Integer;            {used internally}
    cntRel:             Integer;            {reserved}
    maxRel:             Integer;            {reserved}
    cntNRel:            Integer;            {reserved}
    maxNRel:            Integer;            {reserved}
    cntEmpty:           Integer;            {reserved}
    cntHandles:         Integer;            {reserved}
    minCBFree:          LongInt;            {reserved}
    purgeProc:          ProcPtr;            {purge-warning procedure}
    sparePtr:           Ptr;                {used internally}
    allocPtr:           Ptr;                {used internally}
    heapData:           Integer;            {first usable byte in zone}
END;
THz = ^Zone;                                {zone pointer}
bkLim
A pointer to the byte following the last byte of usable space in the zone.
purgePtr
Used internally.
hFstFree
A pointer to the first free master pointer in the zone. All master pointers that are allocated but not currently in use are linked together into a list. The hFstFree field references the head node of this list. The Memory Manager updates this list every time it allocates a new relocatable block or releases one, so that the list contains all unused master pointers. If the Memory Manager needs a new master pointer but this field is set to NIL , it allocates a new nonrelocatable block of master pointers. You can check the value of this field to see whether allocating a relocatable block would cause a new block of master pointers to be allocated.
zcbFree
The number of free bytes remaining in the zone. As blocks are allocated and released, the Memory Manager adjusts this field accordingly. You can use the FreeMem function to determine the value of this field for the current heap zone.
gzProc
A pointer to a grow-zone function that system software uses to maintain control over the heap. The system's grow-zone function subsequently calls the grow-zone function you specify for your heap, if any. You can change a heap zone's grow-zone function at any time but should do so only by calling the InitZone or SetGrowZone procedures. Note that in current versions of system software, this field does not contain a pointer to the grow-zone function that your application defines.
moreMast
The number of master pointers the Memory Manager should allocate at a time. The Memory Manager allocates this many automatically when a heap zone is initialized. By default, master pointers are allocated 32 at a time for the system heap zone and 64 at a time for the application heap zone, but this might change in future versions of system software.
flags
Used internally.
cntRel
Reserved.
maxRel
Reserved.
cntNRel
Reserved.
maxNRel
Reserved.
cntEmpty
Reserved.
cntHandles
Reserved.
minCBFree
Reserved.
purgeProc
A pointer to the zone's purge-warning procedure, or NIL if there is none. The Memory Manager calls this procedure before it purges a block from the zone. Note that whenever you call the Resource Manager procedure SetResPurge with the parameter set to TRUE , the Resource Manager installs its own purge-warning procedure, overriding any purge-warning procedure you have specified here.
sparePtr
Used internally.
allocPtr
Used internally.
heapData
A dummy field marking the beginning of the zone's usable memory space. The integer in this field has no significance in itself; it is just the first 2 bytes in the block header of the first block in the zone. For example, if myZone is a zone pointer, then @(myZone^.heapData) is the address of the first usable byte in the zone, and myZone^.bkLim is a pointer to the byte following the last usable byte in the zone.

The structure of a heap zone is the same in both 24-bit and 32-bit addressing modes. The use of several of the fields that are reserved or used internally, however, may differ in 24-bit and 32-bit heap zones.


© 1997 Apple Computer, Inc.

Previous | Chapter Top | Chapter Contents | Next